<?php

namespace app\controllers;

use app\models\Good;
use app\models\Cate;
use app\tools\Re;
use Yii;

class CateController extends BaseController
{
    //分类列表
    public function actionList(){
      $good_list = Good::find()->where(['status' => 1])->all();
      //var_dump($good_list);die;
      $data = [];
      if ($cate_one_list) {
        foreach ($cate_one_list as $v) {
          $cate_two_list = Cate::find()->where(['status' => 1, 'pid' => $v->id])->orderBy('sort desc, created_at desc')->all();
          $cate_two_data = [];
          if ($cate_two_list) {
            foreach ($cate_two_list as $v1) {
              $cate_two_data[] = [
                'id' => $v1->id,
                'name' => $v1->name,
                'logo' => $v1->logo,
              ];
            }
          }
          $data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'cover' => $v->cover,
            'cate_two_list' => $cate_two_data,
          ];
        }
      }
      return Re::data(['data' => $data]);
    }

    //二级分类列表(根据二级分类获取所有兄弟分类)
    public function actionTwoCateList(){
      $cate_id = Yii::$app->request->post('cate_id', 0);
      $cate_info = Cate::findOne($cate_id);
      if (empty($cate_id) || empty($cate_info)) {
        return Re::data(null, '分类信息读取错误', Re::ERROR);
      }
      $cate_list = Cate::find()->where(['status' => 1, 'pid' => $cate_info->pid])->orderBy('sort desc, created_at desc')->all();
      $cate_data = [];
      if ($cate_list) {
        foreach ($cate_list as $v) {
          $cate_data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'sub_name' => $v->sub_name,
          ];
        }
      }
      return Re::data(['data' => $cate_data]);
    }
}
